package com.wn34.goods.controller;

import com.github.pagehelper.PageInfo;
import com.wn34.goods.entity.GoodsType;
import com.wn34.goods.entity.User;
import com.wn34.goods.service.GoodsService;
import com.wn34.goods.service.OrderService;
import com.wn34.goods.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderService orderService;

    @RequestMapping("/goodsList")
    public String goodsList(String goodsName,
                            Integer typeId,
                            @RequestParam(value = "now", defaultValue = "1") Integer now,
                            @RequestParam(value = "size", defaultValue = "4") Integer size,
                            Map map) {
        now = now == 0 ? 1 : now;
        PageInfo<GoodsVo> pageInfo = goodsService.goodsPage(now, size, goodsName, typeId);
        List<GoodsType> goodsTypes = goodsService.goodsTypes();
        map.put("pageInfo", pageInfo);
        map.put("goodsName", goodsName);
        map.put("typeId", typeId);
        map.put("goodsTypes", goodsTypes);
        return "forward:/main.html";
    }

    @RequestMapping("/orders")
    @ResponseBody
    public String orders(Integer gid, HttpServletRequest req){
        User user = (User) req.getSession().getAttribute("loginUser");
        int i = orderService.saveOrders(user.getUserid(), gid, 1);
        return i + "";
    }
}
